﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Drawing.Drawing2D;


namespace testing_students
{

    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            lblQuestion.Text = "Добро пожаловать в программу тестирования студентов! Для прохождения теста нажмите 'Далее'";
            nextButton.Text = "Далее";
            
            lblQuestion.MaximumSize=new Size(700,0);
            int y = 150;
            
                for (int i = 0; i < MAX_ANSWERS; i++)
                {
                    rbAnswers[i] = new RadioButton();
                    rbAnswers[i].Location = new Point(280, y);
                    rbAnswers[i].Visible = false;
                    rbAnswers[i].AutoSize = true;
                    this.Controls.Add(rbAnswers[i]);
                    y += 40;
                }

                y = 150;

                for (int i = 0; i < MAX_ANSWERS; i++)
                {
                    cbAnswers[i] = new CheckBox();
                    cbAnswers[i].Location = new Point(280, y);
                    cbAnswers[i].Visible = false;
                    cbAnswers[i].AutoSize = true;                   
                    this.Controls.Add(cbAnswers[i]);
                    y += 40;
                }
        }

        private const int MAX_ANSWERS = 6;
        List<Test> list_of_questions = new List<Test>();
        
        

        RadioButton[] rbAnswers = new RadioButton[MAX_ANSWERS];
        CheckBox[] cbAnswers = new CheckBox[MAX_ANSWERS];

        private void MainForm_Load(object sender, EventArgs e)
        {
            try
            {
                StreamReader sr = new StreamReader("Questions.txt", System.Text.Encoding.GetEncoding(1251));
                string s;
                string[] stArray;
                string[] answers;
                int[] rightAnswers;

                while (!sr.EndOfStream)
                {
                    s = sr.ReadLine();
                    stArray = s.Split('@');

                    answers = new string[int.Parse(stArray[1])];
                    rightAnswers = new int[int.Parse(stArray[2])];


                    for (int i = 0; i < answers.Length; i++)
                        answers[i] = sr.ReadLine();

                    for (int i = 0; i < rightAnswers.Length; i++)
                        rightAnswers[i] = int.Parse(sr.ReadLine());

                    list_of_questions.Add(new Test(stArray[0], answers, rightAnswers));
                }
                sr.Close();
            }
            catch (Exception ex)
            {
                string str = "Ошибка: " + ex.Message;
                MessageBox.Show(str, "Почему не запускается программа", MessageBoxButtons.OK, MessageBoxIcon.Information); 
            }

        }

        int iCorrect = 0;
        int iQuestion = -1;
        private void nextButton_Click(object sender, EventArgs e)
        {
            if (iQuestion == 8)
            { nextButton.Text = "Узнать результат"; }
            else if (iQuestion > -1)
            {
                CheckingAnswer(iQuestion);
            }
            Renaming(++iQuestion);

            
        }

        private void Renaming(int iQuestion)
        {
            if (iQuestion < list_of_questions.Count)
            {
                
                lblQuestion.Text = list_of_questions[iQuestion].Question;
               

                for (int i = 0; i < MAX_ANSWERS; i++)
                {
                    rbAnswers[i].Visible = false;
                    cbAnswers[i].Visible = false;
                }

                for (int i = 0; i < list_of_questions[iQuestion].Answers.Length; i++)
                {                    
                    if (list_of_questions[iQuestion].Correct.Length == 1)
                    {
                        rbAnswers[i].Visible = true;
                        rbAnswers[i].Text = list_of_questions[iQuestion].Answers[i];
                        rbAnswers[i].MaximumSize = new Size(1000, 0);

                    }
                    if (list_of_questions[iQuestion].Correct.Length >1)
                    {
                        cbAnswers[i].Visible = true;
                        cbAnswers[i].Text = list_of_questions[iQuestion].Answers[i];
                        cbAnswers[i].MaximumSize = new Size(1000, 0);
                    }
                
                }

                for (int i = list_of_questions[iQuestion].Answers.Length; i < MAX_ANSWERS; i++)
                {
                    rbAnswers[i].Visible = false;
                    cbAnswers[i].Visible = false;
                }
                
              
            }
            else
            {
                string str = "Количество правильных ответов: " + iCorrect.ToString();
                MessageBox.Show(str, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ActiveForm.Close();
            }
        }

        int[] buttons_pressed;
        private void CheckingAnswer(int iQuestion)
        {
            
            int n = 0;
            if (list_of_questions[iQuestion].Correct.Length == 1)
            {
                buttons_pressed = new int[1];  
                for (int i=0; i<rbAnswers.Length; i++)
                {
                    if (rbAnswers[i].Checked == true) 
                        {
                              
                            buttons_pressed[0] = i;
                            
                        }
                    }
                }
            else
            {
                int k=0;
                
                for (int i = 0; i < cbAnswers.Length; i++)
                {
                    
                    if (cbAnswers[i].Checked == true)
                        {
                            k++; 
                           
                        }
                }
                buttons_pressed = new int[k];
                for (int i = 0; i < cbAnswers.Length; i++)
                {

                    if (cbAnswers[i].Checked == true)
                    {
                        buttons_pressed[n] = i;
                        n++;
                    }
                }
            }
             if (list_of_questions[iQuestion].AnswerIsTrue (buttons_pressed) == true)
                 iCorrect++;
            }

        private void MainForm_Paint(object sender, PaintEventArgs e)
        {
            GraphicsPath rus = new GraphicsPath();
            GraphicsPath a = new GraphicsPath();
            string strrus = "Русский язык";
            string strletter = "A";
            FontFamily rusfamily = new FontFamily("Cambria");
            FontFamily letterfamily = new FontFamily("Algerian");
            int rusfontStyle = (int)FontStyle.Italic;
            int afontStyle = (int)FontStyle.Regular;
            int russize = 50;
            int lettersize = 290;
            Point rpoint = new Point(15, 10);
            Point apoint = new Point(0, 100);
            StringFormat format = StringFormat.GenericDefault;
            rus.AddString(strrus,
                rusfamily,
                rusfontStyle,
                russize,
                rpoint,
                format);
            a.AddString(strletter,
              letterfamily,
              afontStyle,
              lettersize,
              apoint,
              format);
            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.FillPath(Brushes.Blue, rus);
            e.Graphics.DrawPath(new Pen(Brushes.Black, 2), rus);
            e.Graphics.FillPath(Brushes.Red, a);
            e.Graphics.DrawPath(new Pen(Brushes.Black, 2), a);

        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Вы уверены, что хотите выйти из приложения?", "Exit", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK) this.Close();
        }

      
      
   
        }
       
    }



